<?php

namespace App\Http\Controllers\Special;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Special;
use App\Models\Tags;
use App\Models\Type;
use App\Contant\TagsType;
use App\Models\Article;

class SpecialController extends Controller
{
    /**
     * 专题页
     */
    public function index()
    {
        $data = Special::orderby("orderby","desc")->orderby("id","desc")->get();
        $this->ret['list'] = $data;
       
        return view("special.index",$this->ret);
    }

    /**
     * 专题添加文章
     */
    public function create()
    {
       
        return view("special.add");
    }


    //添加专题
    
    public function createSpecial(){
        $Type  = new Type();
        $types = $Type->getListState("ARTICLE");  //获取文章类别
        $tags  = Tags::where('state',1)->where('type',TagsType::ARTICLE)->select('id','title')->get();

        $this->ret['types']= $types;
        $this->ret['tags'] = $tags;
        return view("special.addSpecial",$this->ret);
    }
    /**
     * 存储
     */
    public function store(Request $request)
    {
       // dd($request->input());
        $id =  $request->input("id");
        $data = $request->input();

       
        if($id){
            $special = Special::where('id',$id)->first();
        }else{
            $special = new Special();
        }
        $special->title = $data['title'];
        $special->img_url = $data['img_url'];
        
        $special->bill_url = $data['bill_url'];
        $special->orderby = $data['orderby'];
        $special->likes = $data['likes'];
        $special->view = $data['view'];
        $special->description = $data['description'];
        $special->count_special = $data['count_special'];
        $special->jianshu = $data['jianshu'];

        $special->seo_title = $data['seo_title'];
        $special->seo_description = $data['seo_description'];
        $special->result = $data['result'];
        
        $result = $special->save();
        if($result){
             return redirect('/special/index');
        }
    }

    /* 
     * 
     * ajax修改文章状态
     */
    public function changeState(Request $request){
        $id    = $request->input('id');
        $state = $request->input('state');
        $article = Special::where('id',$id)->first();
        $article->state  = $state;
        if($article->save()){
            return $this->getMessage(0,'修改成功');
        }else{
            return $this->getMessage(1,'修改状态失败');
        }
    }
    /**
     * 删除专题
     */
    public function delete_special(Request $request){
        $id = $request->input('id');
        $result = Special::where('id',$id)->delete();

        if($result){
            return $this->getMessage(0,'删除成功');
        }else{
            return $this->getMessage(1,'删除失败');
        }
    }
    /**
     * 编辑专题
     */
    public function edit_special($id){
        
        $data = Special::where("id",$id)->first();
        $this->ret['data'] = $data;
        return view("special.editSpecial",$this->ret);
    }

    /**
     * 专题课程列表
     */
    public function articleList($id){
            
        $title = Special::where("id",$id)->select("title")->first();
       
        $data = Article::where("special_id","like","%".$id."%")->get();
        $this->ret['data'] = $data;
        $this->ret['id'] = $id;
        $this->ret['title'] = $title;
        return view("special.list",$this->ret);

    }

    /**
     * 删除文章 $aid 文章id || $id 专题id
     */
    public function articleDel($aid,$id){
        //dd($id);
         
         $data = Article::where("id",$aid)->first();
         $special = $data->special_id;
         $arr = explode(",",$special);
         $array = array_diff($arr, [$id]);
         $separated = implode(",", $array);
         $data->special_id = $separated;
         $result = $data -> save();
         if($result){
            return redirect('/special/articleList/'.$id);
         }
         dd($result);

    }

    /*
     * 推送
     */
    public function thematicPush(Request $request){

        return view('special.push');
    }

}
